<?php
/**
 * Q_Loader
 * Allows the use of custom filters/rules. Register a namespace with a directory
 * and the rules/filters will be loaded from that location rather than from the
 * include path.
 */
class Q_Loader {

    static protected $namespaces = array();
    static protected $autoloaderRegistered = false;
    
    static public function registerNamespace($namespace, $path) {
    
        self::$namespaces[$namespace] = realpath($path);
        if (!self::$autoloaderRegistered) {
            self::registerAutoloader();
        }
    
    }
    
    /**
     * Registers the autoloader method with spl
     * @todo This adds the autoload to the top of the stack, which I would prefer it not do
     *       but if it isn't on the top of the stack, you get warnings.
     */
    static public function registerAutoloader() {
    
        spl_autoload_register(array('Q_Loader', 'autoload'), true, true);
    
    }
    
    static protected function autoload($class) {
    
        foreach (self::$namespaces as $namespace => $path) {
            $namespace = rtrim($namespace, '_') . '_';
            $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
            $classEnding = substr($class, strlen($namespace));
            $classPath = $path . $classEnding . '.php';
            if (file_exists($classPath)) {
                require_once $classPath;
            }
        }
    
    }

}
